<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class Admin_GroupController extends Zend_Controller_Action
{

    private $_groupService;

	public function init()
    {
        $this->_groupService = $this->_helper->getService('Core_Service_Group');
    }

    public function indexAction()
    {
        $this->view->groups = $this->_groupService->fetchAll();
    }

    public function createAction()
    {
        $this->_helper->acl()->isAllowed('service:group', 'create');

        $form = new Admin_Form_Group_Create();
        $this->view->form = $form;

        $request = $this->getRequest();
        if ($request->isPost() && $form->isValid($request->getPost())) {
            //$groupName = $form->getValue('group_name');
            //$result = $this->_groupService->create($groupName);
            $result = $this->_groupService->create($form->getValues());
            if ($result) {
                $this->view->flashMessage()->addMessage('Ny brukergruppe med navn <b>' . $groupName . '</b> ble opprettet', RedCross_View_Helper_FlashMessage::SUCCESS);
                return $this->_helper->redirector('index');
            } else {
                // Something went wrong with the model or database insertion
                $this->view->error = true;
            }
        }
    }

    public function editAction()
    {
        $this->_helper->acl()->isAllowed('service:group', 'edit');

        $groupId = $this->_getParam('id');

        $validator = new Zend_Validate_Int();
        if (!$validator->isValid($groupId)) {
            throw new RedCross_Exception();
        }
        $groupId = (int) $groupId;
	    $this->view->groupId = $groupId;
        
        $group = $this->_groupService->fetchById($groupId);
        if (!$group) {
        	throw new RedCross_Exception_NotFound();
        }

        $editForm = new Admin_Form_Group_Edit();
        $editForm->populate(array('name' => $group->name, 'is_forum_group' => $group->is_forum_group));

        $addUserForm = new Admin_Form_Group_AddUser();
        $addUserForm->fillForm($group->id);

        $request = $this->getRequest();
        if ($request->isPost()) {
            $values = $request->getPost();
            if (isset($values['edit_group']) && $editForm->isValid($values)) {
                $this->view->editSuccess = $this->_groupService->edit($group, $editForm->getValues());
            } else if (isset($values['add_user']) && $addUserForm->isValid($values)) {
                // Add user and remove it from the list of available users
                $this->view->addedUser = $this->_groupService->addUser($group->id, $addUserForm->getValue('name'));
                $addUserForm->getElement('name')->removeMultiOption($addUserForm->getValue('name'));

                // Update the registered members table
                $group = $this->_groupService->fetchById($groupId);
            }
        }

        $this->view->group = $group;
        $this->view->editForm = $editForm;
        $this->view->addMemberForm = $addUserForm;
    }

    public function deleteAction()
    {
        $this->_helper->acl()->isAllowed('service:group', 'delete');

    	$groupId = $this->_getParam('id');
        
    	$validator = new Zend_Validate_Int();
    	if (!$validator->isValid($groupId)) {
    		throw new RedCross_Exception_ValidationFailed();
    	}
        $groupId = (int) $groupId;
	
    	$success = $this->_groupService->delete($groupId);
        if($success) {
            $this->view->flashMessage()->addMessage('Brukergruppen ble slettet', RedCross_View_Helper_FlashMessage::SUCCESS);
        } else {
            $this->view->flashMessage()->addMessage('Klarte ikke å slette brukergruppen', RedCross_View_Helper_FlashMessage::ERROR);   
        }
    	$this->_helper->redirector('index');
    }

    public function removeUserAction()
    {
        $groupId = $this->_getParam('id');
        $userId = $this->_getParam('user');

        $validator = new Zend_Validate_Int();
        if (!$validator->isValid($groupId) || !$validator->isValid($userId)) {
            throw new RedCross_Exception_ValidationFailed();
        }
        $groupId = (int) $groupId;
        $userId = (int) $userId;

        $this->_groupService->removeUser($groupId, $userId);
        // Add flashmessage before redirecting
        $this->view->flashMessage()->addMessage('Fjernet en bruker fra gruppen', RedCross_View_Helper_FlashMessage::SUCCESS);
        return $this->_helper->redirector('edit', null, null, array('id' => $groupId));
    }

}
